#ifndef __WINGBOT_USART_H
#define __WINGBOT_USART_H

#include "Driver_USART.h"
#include "wingbot_queue.h"
#include "RTE_Device.h"
#include "GPIO_STM32F10x.h"

#define USART_TX_TIMEOUT    (uint32_t)20000U //usart超时时间
#define UART_MAX_STRING     (uint32_t)2000U   //一次性发送的最长字符串长度

#if defined __cplusplus
extern "C" {
#endif

    typedef enum _TRANS_STATUS {
      TRANS_OK,
      TRANS_TIMEOUT=1,
      TRANS_IDLE,
      TRANS_WAITING,
      TRANS_FINISH
    } TRANS_STATUS;
    
    typedef void (*USART_IRQ)(void);        //中断处理程序入口
    
    typedef struct _USART_TRANS_INFO{
        Queue_T                *rx_que;        // 接收队列
        uint32_t                rx_tick;       // 接收计时
        uint32_t                rx_cnt;        // 接收计数
        uint32_t                len;           // 预期的长度
        TRANS_STATUS            rx_state;      // 接收状态
        bool                    rx_dataReady;  // 数据是否准备好
    }USART_TRANS_INFO;

    typedef struct _USART_INFO {
        USART_TypeDef          *port;
        USART_IRQ               irq_handler;   // 中断处理
        uint32_t                baudrate;      // Baudrate
        uint16_t                parity;        // 奇偶校验
        uint16_t                stopbits;      // 停止位
        USART_TRANS_INFO        trans_info;
    } USART_INFO;
    
    void wing_usart_init(USART_TypeDef *USARTx,
                      uint32_t baudrate,
                      uint16_t parity,
                      uint16_t stopbits);
    void wing_usart_nvic_init(USART_TypeDef *USARTx);
    TRANS_STATUS wing_usart_send_byte(USART_TypeDef* USARTx,unsigned char data);
    void wing_usart_send_bytes(USART_TypeDef* USARTx, uint8_t* p, uint16_t len);
    void wing_usart_send_string(USART_TypeDef* USARTx, const char* msg);
    void wing_clear_buffer(u8* p, u16 len);
    
#if defined __cplusplus
}
#endif

#endif
